package common.page
{
	import mx.controls.Alert;
	import mx.controls.Label;
	import mx.events.ItemClickEvent;
	
	public class PageConf
	{
		/** 分页类开始.*./ /** 排序字段. */
		public var sortField:String;
		/** 排序升降.des,aesc. */
		public var sortDir:String;
		/** 分页开始. */
		private var start:Number;
		/** 分页结束. */
		private var end:Number;
		/** 每页最大数. */
		private var limit:Number;
		/** 分页总数. */
		public var total:Number;
	    
		public var canSearch:Boolean;
		public function setStart(start:Number):void {
			 this.start = start;;
		}
		public function getStart():Number {
			return this.start;
		}
		public function getEnd():Number {
			//每次都重新计算End.因为.有可能在查询时,数据增加.
			return Math.min(this.start + this.limit, this.total);
		}
		public function getLimit():Number{
			return this.limit;
		}
		
		public function getCurrPage():Number {//得到当前页.
			return Number(Math.floor(start / limit));
		}
	
		public function getTotalPage():Number {
			/** 取得总共的页数. */
			return Number(Math.ceil(total / limit));
		}
	
		public function getGo(page:Number):void {/* 页面跳转函数.输入要跳转的页面. */
			var totalPage:Number = getTotalPage();
			var currentPage:Number = getCurrPage();
			if (page <= 1) {/* 如果小于1.跳到首页. */
				getFirst();
				return;
			} else if (page >= totalPage) {/* 如果大于总页数跳到末页. */
				getLast();
				return;
			} else if (page == currentPage) {/* 当前页返回. */
				return;
			} else {/* 否则进行页面跳转.也就是计算start和end. */
				this.start = (page - 1) * limit;
				this.end = Math.min(page * limit, total);
			}
		}
	
		public function getNext():void  {
			trace("getNextPage");
			var currentPage:Number = getCurrPage();
			var tempStart:Number = (currentPage + 1) * limit;
			if (tempStart >= total)
				return;
			this.start = tempStart;
			this.end = Math.min((currentPage + 2) * limit, total);
		}
	
		public function getFirst():void  {
			trace("getFirst");
			this.start = 0;
			this.end = Math.min(limit, total);
		}
	
		public function getLast():void {
			trace("getEnd");
			var lastPage:Number = getTotalPage();
			/** 如果刚好总页数刚好被整除.就往前一页. */
			if ((lastPage * limit) >= total) {
				lastPage--;
			}
			this.start = lastPage * limit;
			this.end = Math.min((lastPage + 1) * limit, total);
		}
	
		public function getPrev():void {
			trace("getPrevpage");
			var currentPage:Number = getCurrPage();
			/** 前一页等于当前页数乘以每页记录数. */
			this.start = Math.max((currentPage - 1) * limit, 0);
			this.end = Math.max((currentPage) * limit, limit);
		}
	
		public function hasNext():Boolean{
			canSearch = false;
			if (this.getCurrPage()+1 == this.getTotalPage()) {
				return false;
			} else if (this.getTotalPage() <= 1) {
				return false;
			} else {
				canSearch = true;
				return true;
			}
		}
	
		public function hastPrev():Boolean {
			canSearch =false;
			if (this.start == 0) {
				return false;
			} else {
				canSearch = true;
				return true;
			}
		}
	
		public function PageConf(start:Number, limit:Number, total:Number) {/* 构造函数.设置开始.和分页数. */
			this.start = start;
			this.limit = limit;
			this.end = this.limit;
			this.total = total;
		}
		
		public function pageItemClick(itemClick:ItemClickEvent, pageInfo:Label):void {
				if(itemClick.index == 0){
					if(hastPrev()){
						getFirst();
					}
					  
				}else if(itemClick.index == 1){
					if(hastPrev()){
						getPrev();
					}
				}else if(itemClick.index == 2){
					if(hasNext()){
						getNext();
					}
					
				}else if(itemClick.index == 3){
					if(hasNext()){
						getLast();
					}
				}
				
	    }
	    
	    public function showPageInfo(pageInfo:Label):void {//通用显示Info页面.
			var curPage:Number = getCurrPage();
			var end:Number = getEnd();
			if(end==0){
				curPage = 0;
			}else {
				curPage = curPage+1;
			}
				pageInfo.text = "[记录数："+end+"/"+total+",页数:"+(curPage)+"/"+getTotalPage()+"]";
	    }
	    
	    public function toString():String{
	    	return "start["+start+"]end["+
	    	end+"]total["+total+"]limit["+limit;
	    }
	}
}